package com.wd.live.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import java.lang.reflect.ParameterizedType

/**
 * 作者: 刘文豪
 * 日期: 2024/6/19 20:17
 * 功能：
 */
abstract class BaseActivity <V:ViewModel,D:ViewDataBinding>(private val layout:Int):AppCompatActivity(){
    lateinit var viewModel:V
    lateinit var dataBinding:D
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel=ViewModelProvider(this)[(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<V>]
        dataBinding=DataBindingUtil.setContentView(this,layout)
        dataBinding.lifecycleOwner=this
        initView()
        dataObserve()
    }

    abstract fun dataObserve()

    abstract fun initView()
    override fun onDestroy() {
        super.onDestroy()
        if(dataBinding!=null){
            dataBinding.unbind()
        }
    }
}